import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:get/instance_manager.dart';
import 'package:get/route_manager.dart';
import 'package:sudoku/page/HowPage.dart';
import 'package:sudoku/page/GamePage.dart';
import 'package:sudoku/page/LevelDialog.dart';
import 'package:sudoku/page/SetPage.dart';
import 'package:sudoku/util/image_util.dart';
import 'package:sudoku/util/sukodu/generator.dart';
import 'package:sudoku/util/user_preferences.dart';
import 'package:sudoku/util/widget_util.dart';
import 'package:sudoku/widget/RecordDialog.dart';
import 'package:sudoku/widget/ResetDialog.dart';


class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          commonBackground(),
          Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              actions: [
                Padding(
                  padding: const EdgeInsets.only(right: 10),
                  child: InkWell(
                    onTap: () {
                      Get.to(() => SetPage());
                    },
                    child: loadAssetImage("setting", width: 40),
                  ),
                )
              ],
            ),
            body: SafeArea(
                child: SizedBox(
              width: double.infinity,
              height: double.infinity,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                    top: 20,
                    child: loadAssetImage("sudoku", width: Get.width * 0.7),
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      _btnWidget("历史记录", () {
                        showRecordDialog();
                      }),
                      const SizedBox(
                        height: 25,
                      ),
                      _btnWidget("玩法说明", () {
                        Get.to(() => HowPage());
                      }),
                    ],
                  ),
                  Positioned(
                    bottom: 20,
                    child: InkWell(
                      onTap: () {
                        showLevelDialog().then((value) {
                          if (value != null) {
                            Get.to(
                              () => GamePage(value),
                              curve: Curves.ease,
                              duration: const Duration(milliseconds: 600),
                              transition: Transition.rightToLeftWithFade,
                            );
                          }
                        });
                      },
                      child: loadAssetImage("start"),
                    ),
                  ),
                ],
              ),
            )),
          )
        ],
      ),
    );
  }

  Widget _btnWidget(String title, VoidCallback clickCallback) {
    return InkWell(
      onTap: clickCallback,
      child: SizedBox(
        width: Get.width * 0.6,
        child: Stack(
          alignment: Alignment.center,
          children: [
            loadAssetImage("btn_bg"),
            Text(
              title,
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 30,
                  fontFamily: "Han",
                  shadows: [
                    BoxShadow(
                        color: Colors.white54,
                        offset: Offset(1, 5),
                        blurRadius: 20,
                        spreadRadius: 0)
                  ],
                  fontWeight: FontWeight.bold),
            )
          ],
        ),
      ),
    );
  }
}
